<template>
	<view class='page'>
		<view class="info">
			<view class="info-box">
				<view class="line flex-between-center">
					<view class="label">营业日</view>
					<view class="text flex-end-center">
						<u-radio-group v-model="week" placement="row" @change="weekChange">
							<u-radio :name='0' label="每天营业"></u-radio>
							<u-radio :name='1' label="自定义"></u-radio>
						</u-radio-group>
					</view>
				</view>
				<weekTime :data="time[0].weekTime" v-if="week" :hasRest="true" :hasAdd="true"
					@bindTimeChange="bindTimeChange"></weekTime>
			</view>

			<view class="info-box" v-if="!week">
				<view class="line flex-between-center">
					<view class="label">营业时间段</view>
					<view class="text flex-end-center">
						<u-radio-group v-model="period" placement="row" @change="timeChange">
							<u-radio :name="0" label="全天"></u-radio>
							<u-radio :name="1" label="选择时间段"></u-radio>
						</u-radio-group>
					</view>
				</view>
				<dateTime :data="time[0].timeArr" v-if="period" :hasRest="true" :hasAdd="true"
					@bindTimeChange="bindTimeChange"></dateTime>
			</view>

		</view>
		<!-- 固定每日时间 -->

		<u-button type="primary" text="设置" @click="onsubmit"></u-button>
	</view>

</template>

<script>
	import cookie from '../../../utils/store/cookie';
import dateTime from './components/dateTime';
	import weekTime from './components/weekTime';
	export default {
		components: {
			dateTime,
			weekTime
		},
		data() {
			return {
				week: 0,
				period: 0,
				time: [{
					timeArr: [{
						start: "00:00",
						end: "23:59"
					}], //每日固定时间
					weekTime: [], //自定义时间 //setWeekTime
				}, ],
			}
		},
		onShow() {
			this.setWeekTime();
		},

		methods: {
			timeChange(e) {
				if (!e) {
					this.time[0].timeArr = [{
						start: "00:00",
						end: "23:59"
					}]
				}
			},
			weekChange(e) {
				if (!e) {
					this.setWeekTime()
				} else {
					this.period = 0
				}
			},
			onsubmit() {
				// console.log(this.verifierTime())
				let data = {}
				let weekList = this.time[0].weekTime
				let timeList = this.time[0].timeArr
				if (this.week == 1) {
					console.log('this.week', this.week)
					weekList.forEach((item, index) => {
						if (!item.check) return
						let stf = ''
						item.time.forEach((item1, index1) => {
							if (index1 != item.time.length - 1) {
								stf = stf + item1.start + '-' + item1.end + ','
							} else {
								stf = stf + item1.start + '-' + item1.end
							}

						})
						data[item.name] = stf
					})
				} else {
					weekList.forEach((item, index) => {

						let stf = ''
						timeList.forEach((item1, index1) => {
							if (index1 != timeList.length - 1) {
								stf = stf + item1.start + '-' + item1.end + ','
							} else {
								stf = stf + item1.start + '-' + item1.end
							}
							console.log(stf)
						})
						data[item.name] = stf
					})
				}
			  const cacheAuthStore = cookie.get('cacheAuthStore')
			  if (cacheAuthStore) {
				  cacheAuthStore.form.dayTime = JSON.stringify(data)
				  cookie.set('cacheAuthStore', cacheAuthStore)
			  }
			  this.$yrouter.push({
			  	path: '/userPack/pages/user/AuthStore/index',
			  })
				// 设置本地缓存的cacheAuthStore 下的 dayTime
				
			},
			setWeekTime() {
				let weekTime = [],
					weekName = ['一', '二', '三', '四', '五', '六', '日'];
				for (let i = 0; i < 7; i++) {
					weekTime.push({
						check: true,
						day: i + 1,
						name: '周' + weekName[i],
						time: [{
							start: '00:00',
							end: '23:59'
						}]
					})
				}
				this.time[0].weekTime = JSON.parse(JSON.stringify(weekTime));
				// this.time[1].weekTime = JSON.parse(JSON.stringify( weekTime));
			},
			verifierTime() {
				//全部时间段校验
				let arr = [],
					arr2 = [],
					todoArr = [],
					wrongList = [],
					bool = true

				if (this.period === 1) {
					todoArr = this.time[0].timeArr;

					todoArr.map((item) => arr.push(item.start, item.end));

					wrongList = arr.filter((item, index) => {
						if (index > 0) {
							let preArr = arr[index - 1].split(":"),
								curArr = item.split(":");
							let pre = Number(preArr[0] * 3600) + Number(preArr[1] * 60); //+ Number(preArr[2]);
							let cur = Number(curArr[0] * 3600) + Number(curArr[1] * 60); //+ Number(curArr[2]);;
							// if (pre > cur) {
							// 	wx.showToast({
							// 		title: `在时间段第${index+1}项输入有误`,
							// 		icon: "none",
							// 		duration: 2000,
							// 	});
							// 	bool = false
							// }
						}
					});
				}
				if (this.week === 1) {
					//获取自定义营业日
					todoArr = this.time[0].weekTime;
					//创建对应营业日空数组用于存放时间
					todoArr.map((item) => arr2.push({
						name: item.name,
						check: item.check,
						time: []
					}));
					//循环输入营业时间段
					todoArr.forEach((item, index) => {
						item.time.forEach((item1, index1) => {
							arr2[index].time.push(item1.start, item1.end)
						})
					})
					let temp = [];
					try {
						arr2.forEach((week, weekIndex) => {
							// week.time.forEach((item,index)=>{
							if (!week.check) return

							temp = week.time.filter((item1, index1) => {
								if (index1 > 0) {
									let preArr = week.time[index1 - 1].split(":"),
										curArr = item1.split(":");
									let pre = Number(preArr[0] * 3600) + Number(preArr[1] *
										60); //+ Number(preArr[2]);
									let cur = Number(curArr[0] * 3600) + Number(curArr[1] *
										60); //+ Number(curArr[2]);;
									// if (pre > cur) {
									// 	wx.showToast({
									// 		title: `在${week.name}第${index1+1}项输入有误`,
									// 		icon: "none",
									// 		duration: 2000,
									// 	});
									// 	bool = false
									// }
								}
							});
							// })

							if (!bool) throw new Error("EndIterative");
						})
					} catch (e) {}
				}

				return bool
			},
			bindTimeChange(e, status) {
				if (status == 1) {
					this.time[0].weekTime = e
				}
				if (status == 2) {
					this.time[0].timeArr = e
				}
			},
		},
	}
</script>

<style lang='scss' scoped>
	@import "./index.scss";

	/deep/.u-radio {
		padding: 15rpx 20rpx;
	}
</style>